package com.hou.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.commonutils.JwtUtils;
import com.hou.commonutils.R;
import com.hou.eduorder.entity.Order;
import com.hou.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 生成订单
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        String orderNo = orderService.createOrders(courseId, memberId);
        return R.ok().data("orderId",orderNo);
    }

    // 查询订单
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);

        Order order = orderService.getOne(queryWrapper);

        return R.ok().data("order",order);

    }

    // 判断课程是否被购买
    @GetMapping("courseIsBuy/{userId}/{courseId}")
    public Boolean courseIsBuy(@PathVariable String userId, @PathVariable String courseId){

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",userId);
        queryWrapper.eq("status",1);

        int count = orderService.count(queryWrapper);

        if (count <= 0) {
            return false;
        } else {
            return true;
        }

    }
}

